<!--
 * @Description: 
 * @Date: 2024-01-17 08:53:28
 * @LastEditTime: 2024-02-18 11:14:17
-->
<template>
  <el-dialog
    title="交办"
    :visible.sync="dialogVisible"
    class="qc-config-dialog"
    width="800px"
    :append-to-body="true"
    @open="handleOpen"
  >
    <el-form
      :model="assignForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-row :gutter="20">
        <div v-for="item in ruleList" :key="item.unId">
          <p class="assign-title">{{ item.name }}:</p>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="asTaskSCreateTime">
              <span class="showTime">{{ item.startTime }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="asTaskSEndTime">
              <span class="showTime">{{ item.endTime }}</span>
            </el-form-item>
          </el-col>
        </div>
        <el-col :span="24">
          <el-form-item label="督办单位">
            <span class="showTime">{{ chSuperviseName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="承办单位" prop="deptId">
            <el-select
              v-model="assignForm.deptId"
              filterable
              placeholder="请输入承办单位"
              style="width: 100%"
            >
              <el-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="联系人" prop="unUserName">
            <el-input
              v-model="assignForm.unUserName"
              placeholder="请输入联系人"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人电话" prop="unPhone">
            <el-input
              v-model="assignForm.unPhone"
              placeholder="请输入联系人电话"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="交办意见" prop="asSuggest">
            <el-input
              v-model="assignForm.asSuggest"
              type="textarea"
              :rows="4"
              placeholder="请输入交办意见"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm"> 交办通过</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  processInfoProId,
  updateProcessInfo,
  userListByRoleId,
} from "@/api/suggest/index";
import { UndertakeInfoList } from "@/api/stage/index.js";
import { parseTime } from "@/utils/index";
export default {
  name: "checkDialog",
  props: ["value", "type", "proId"],
  data() {
    var valiNumber = (rule, value, callback) => {
      //包含小数的数字
      let reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;
      if (value === "") {
        callback(new Error("请输入内容"));
      } else if (!reg.test(value)) {
        callback(new Error("请输入数字"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      assignForm: {
        asTaskSCreateTime: "",
        asTaskSEndTime: "",
        asTaskRCreateTime: "",
        asTaskREndTime: "",
        asTaskECreateTime: "",
        asTaskEEndTime: "",
        deptId: "",
        chSuperviseId: undefined,
        unUserName: "",
        unPhone: "",
        asSuggest: "",
      },
      rules: {
        deptId: [{ required: true, message: "请输入承办单位" }],
        chSuperviseId: [
          { required: true, message: "请输入督办单位", trigger: "change" },
        ],
        unUserName: [{ required: true, message: "请输入联系人" }],
        unPhone: [
          { required: true, message: "请输入联系人电话" },
          { required: true, validator: valiNumber, trigger: "blur" },
        ],
        asSuggest: [{ required: true, message: "请输入交办意见" }],
      },
      userList: [],
      ruleList: [],
      chSuperviseName: "",
    };
  },
  created() {
    userListByRoleId("9").then((res) => {
      this.userList = res.data.users;
    });
    processInfoProId(this.proId).then((res) => {
      this.chSuperviseName = res.data.chSuperviseUser.nickName;
    });
    UndertakeInfoList({
      pageNum: 1,
      pageSize: 100,
    }).then((res) => {
      this.ruleList = res.rows;
      this.ruleList.forEach((item, index) => {
        if (index == 0) {
          item.startTime = this.parseTime(new Date(), "{y}-{m}-{d}");
          this.assignForm.asTaskSCreateTime = item.startTime;
          item.endTime = this.parseTime(
            new Date().setDate(new Date().getDate() + item.limitTime),
            "{y}-{m}-{d}"
          );
          this.assignForm.asTaskSEndTime = item.endTime;
        } else {
          item.startTime = this.parseTime(
            this.ruleList[index - 1].endTime,
            "{y}-{m}-{d}"
          );
          item.endTime = this.parseTime(
            new Date(item.startTime).setDate(
              new Date(item.startTime).getDate() + item.limitTime
            ),
            "{y}-{m}-{d}"
          );
          if (index == 1) {
            this.assignForm.asTaskRCreateTime = item.startTime;
            this.assignForm.asTaskREndTime = item.endTime;
          } else {
            this.assignForm.asTaskECreateTime = item.startTime;
            this.assignForm.asTaskEEndTime = item.endTime;
          }
        }
      });
    });
  },
  methods: {
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          updateProcessInfo({
            ...this.assignForm,
            status: 2,
            proId: this.proId,
          }).then((res) => {
            this.$message.success("交办成功");
            this.dialogVisible = false;
            this.$parent.getList();
          });
        } else {
          return false;
        }
      });
    },
    handleOpen() {
      this.$nextTick(() => {
        this.$refs["ruleForm"].resetFields();
      });
    },
  },
  watch: {
    value: {
      handler(val) {
        this.dialogVisible = val;
      },
      immediate: true,
    },
    dialogVisible(val) {
      this.$emit("input", val); //关键input
    },
  },
};
</script>

<style lang="scss" scoped>
.assign-title {
  padding: 0px 30px 10px 30px;
  font-size: 16px;
  font-weight: bold;
}
.showTime {
  display: inline-block;
  width: 100%;
  height: 100%;
  border: 1px solid #dcdfe6;
  padding: 0 15px;
  border-radius: 4px;
}
</style>
